<template>
  <el-dialog :visible.sync="visible" v-if="visible" :title="title" width="80%" top="8vh">
      <el-form :model="searchForm" inline label-width="80px">
        <el-form-item label="章节名称">
          <el-input v-model="searchForm.catalogName" clearable placeholder="章节名称"/>
        </el-form-item>
        <el-form-item label="时间范围">
          <el-date-picker
            v-model="timeRange"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="章节状态">
          <el-select v-model="searchForm.catalogStatus" clearable placeholder="选择章节状态">
            <el-option label="发表" :value="1"/>
            <el-option label="草稿" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" clearable placeholder="选择状态">
            <el-option label="可用" :value="1"/>
            <el-option label="禁用" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getDataList">搜索</el-button>
          <el-button @click="clearForm">清空</el-button>
        </el-form-item>
      </el-form>
      <div class="btn-group">
        <el-button type="primary" size="mini" @click="openDialog()">添加</el-button>
      </div>
      <el-table :data="dataList" border>
        <el-table-column prop="id" label="编号" align="center"/>
        <el-table-column prop="orderNo" label="排序值" align="center">
          <template slot-scope="scope">
            <el-input class="order-class" v-model="scope.row.orderNo" @blur="saveOrder(scope.row.id,scope.row.orderNo)" />
          </template>
        </el-table-column>
        <el-table-column prop="catalogName" label="章节名称" align="center"/>
        <el-table-column prop="applyTime" label="发表时间" align="center" width="180"/>
        <el-table-column prop="catalogStatus" label="章节状态" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.catalogStatus === 1" size="mini" type="success" @click="changeCatalogStatus(scope.row.id,2)">发表</el-button>
            <el-button v-else size="mini" type="warning" @click="changeCatalogStatus(scope.row.id,1)">草稿</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.status === 1" size="mini" type="success" @click="changeStatus(scope.row.id,2)">可用</el-button>
            <el-button v-else size="mini" type="danger" @click="changeStatus(scope.row.id,1)">禁用</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160px">
          <template slot-scope="scope">
            <el-button size="mini" @click="openDialog(scope.row.id)">修改</el-button>
            <el-button size="mini" @click="deleteInfo(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    <el-pagination hide-on-single-page :current-page="pageInfo.page" :page-size="pageInfo.limit" :page-sizes="[5,10,20,50,100]" @current-change="currentChange" @size-change="sizeChange" layout="total,sizes,prev,pager,next,jumper" :total="pageInfo.total"/>
    <catalog-update ref="catalogUpdate" @closeDialog="getDataList"/>
  </el-dialog>
</template>

<script>
import CatalogUpdate from '@/views/admin/book/catalog/CatalogUpdate.vue'

const initData = {
  catalogName: '',
  catalogStatus: '',
  status: '',
  applyTimeRange: []
}
export default {
  name: 'CatalogList',
  components: { CatalogUpdate },
  data () {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      searchForm: {
        ...initData
      },
      pageInfo: {
        page: 1,
        limit: 5,
        total: 0
      },
      dataList: [],
      visible: false,
      title: '',
      bookId: '',
      timeRange: []
    }
  },
  methods: {
    getDataList () {
      if (this.timeRange?.length > 0) {
        this.searchForm.applyTimeRange = this.timeRange.toString()
      } else {
        this.searchForm.applyTimeRange = ''
      }
      this.$http.get('book/catalog/page', { ...this.searchForm, ...this.pageInfo, bookId: this.bookId }).then(data => {
        if (data) {
          this.dataList = [...data.records]
          this.pageInfo.total = data.total
        }
      }).catch(() => {})
    },
    currentChange (page) {
      this.pageInfo.page = page
      this.getDataList()
    },
    sizeChange (limit) {
      this.pageInfo.limit = limit
      this.getDataList()
    },
    clearForm () {
      this.timeRange = []
      this.searchForm = { ...initData }
      this.pageInfo = {
        page: 1,
        limit: 5,
        total: 0
      }
      this.getDataList()
    },
    changeStatus (id, status) {
      this.$confirm(`确定更改编号<span style="font-weight: bold;color: #66b1ff">[${id}]</span>的状态为${status === 1 ? '<span style="color: green;font-weight: bold;">可用</span>' : '<span style="color: red;font-weight: bold;">禁用</span>'}吗?`, '询问', { dangerouslyUseHTMLString: true }).then(() => {
        this.$http.post('book/catalog/status/info', { id, status }).then(() => {
          this.$message.success('更新状态成功')
          this.getDataList()
        }).catch(() => {})
      }).catch(() => {})
    },
    changeCatalogStatus (id, catalogStatus) {
      this.$confirm(`确定更改编号<span style="font-weight: bold;color: #66b1ff">[${id}]</span>的状态为${catalogStatus === 1 ? '<span style="color: green;font-weight: bold;">发表</span>' : '<span style="color: red;font-weight: bold;">草稿</span>'}吗?`, '询问', { dangerouslyUseHTMLString: true }).then(() => {
        let applyTime = ''
        if (catalogStatus === 1) {
          applyTime = new Date()
        }
        this.$http.post('book/catalog/status/catalog', { id, catalogStatus, applyTime }).then(() => {
          this.$message.success('更新状态成功')
          this.getDataList()
        }).catch(() => {})
      }).catch(() => {})
    },
    deleteInfo (id) {
      this.$confirm(`确定删除编号<span style="font-weight: bold;color: #66b1ff">[${id}]</span>的数据吗?`, '询问', { dangerouslyUseHTMLString: true }).then(() => {
        this.$http.delete(`book/catalog/delete/${id}`).then(() => {
          this.$message.success('删除成功！')
          this.getDataList()
        }).catch(() => {})
      }).catch(() => {})
    },
    openDialog (id) {
      this.$refs.catalogUpdate.init(id, this.bookId)
    },
    saveOrder (id, orderNo) {
      this.$http.post('book/catalog/order', { id, orderNo }).then(() => {
        this.$message.success('更新排序成功！')
        this.getDataList()
      }).catch(() => {})
    },
    init (bookId, title) {
      this.visible = true
      this.title = title
      this.bookId = bookId
      this.getDataList()
    }
  }
}
</script>

<style>
.order-class{
  &>input{
    text-align: center;
  }
}
</style>
